//
//  JiFenNewsController.m
//  PosApp
//
//  Created by 刘萍 on 2018/9/8.
//  Copyright © 2018年 李英新. All rights reserved.
//

#import "JiFenNewsController.h"
#import "JiFenNewsHeadView.h"
#import "JiFenNewsViewCell.h"
#import "JiFenNewsFootView.h"

#import "DuiHuanNewsController.h"
#import "DuiHuanOrderController.h"


@interface JiFenNewsController ()<UITableViewDelegate,UITableViewDataSource,UITextFieldDelegate>
@property (weak, nonatomic) IBOutlet UITableView *JFNewsTable;
@property (strong, nonatomic) JiFenNewsFootView * FootV;
@property (strong, nonatomic) NSMutableArray * JiFenArr;
@property (strong, nonatomic) MBProgressHUD * hud;

@end

@implementation JiFenNewsController

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self.view endEditing:YES];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    self.navigationItem.title = @"积分详情";
    self.JFNewsTable.delegate = self;
    self.JFNewsTable.dataSource = self;
    self.JFNewsTable.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.JFNewsTable registerNib:[UINib nibWithNibName:@"JiFenNewsViewCell" bundle:nil] forCellReuseIdentifier:@"cell"];
    [self UpLoadDataOfJiFenNewsController];
}

- (void)UpLoadDataOfJiFenNewsController{
    NSMutableDictionary * dic = [[NSMutableDictionary alloc] init];
    [dic setObject:self.BankID forKey:@"goods_bank"];
    
    [MainObjectWay PostWithMethod:@"products_list" andAction:@"Points" andAddParam:YES parameters:dic success:^(id responseObject) {
        NSLog(@"积分详情界面数据 == %@",responseObject);
        
        if ([MainObjectWay IsSucceedWithCode:responseObject[@"code"]]) {
            NSMutableArray * arr = responseObject[@"data"][@"list"];
            for (int i = 0; i < arr.count; i ++) {
                NSMutableDictionary * dic = arr[i];
                JiFenNewsModel * model = [JiFenNewsModel mj_objectWithKeyValues:dic];
                [self.JiFenArr addObject:model];
            }
            [self.JFNewsTable reloadData];
        }
    } failure:^(NSError *error) {
        NSLog(@"错误数据 == %@",error);
    }];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.JiFenArr.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 130;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    JiFenNewsViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {
        cell = [[JiFenNewsViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }
    JiFenNewsModel * model = self.JiFenArr[indexPath.row];
    [cell setModel:model];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    JiFenNewsModel * model = self.JiFenArr[indexPath.row];
    DuiHuanOrderController * OrderC = [[DuiHuanOrderController alloc] init];
    OrderC.ShowType = [NSString stringWithFormat:@"%@",model.bank_uppic];
    OrderC.TypeArr = self.JiFenArr;
    OrderC.ChooseID = indexPath.row;
    [self.navigationController pushViewController:OrderC animated:YES];
   
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 40;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    JiFenNewsHeadView * headV = [[NSBundle mainBundle] loadNibNamed:@"JiFenNewsHeadView" owner:self options:nil][0];
    headV.frame = CGRectMake(0, 0, KScreenW, 40);
    headV.backgroundColor = KRGBColor(243, 243, 243);
    headV.ClassLabel.text = [NSString stringWithFormat:@"兑换产品"];
    headV.TitleLabel.text = [NSString stringWithFormat:@"帮您积分变现金"];
    return headV;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 360;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    self.FootV = [[NSBundle mainBundle] loadNibNamed:@"JiFenNewsFootView" owner:self options:nil][0];
    self.FootV.frame = CGRectMake(0, 0, KScreenW, 360);
    self.FootV.JiFenText.delegate = self;
    [self.FootV.DuiHuanBtn addTarget:self action:@selector(ClickOnBtnToDuiHuan) forControlEvents:UIControlEventTouchUpInside];
    return self.FootV;
}

- (void)textFieldDidBeginEditing:(UITextField *)textField{

    [UIView animateWithDuration:0.6 animations:^{
        CGRect Fram = self.view.frame;
        Fram.origin.y = Fram.origin.y-260;
        self.view.frame = Fram;
    } completion:nil];
    
}

- (void)textFieldDidEndEditing:(UITextField *)textField{
    [UIView animateWithDuration:0.6 animations:^{
        CGRect Fram = self.view.frame;
        Fram.origin.y = Fram.origin.y+260;
        self.view.frame = Fram;
    } completion:^(BOOL finished) {
        NSMutableDictionary * dic = [[NSMutableDictionary alloc] init];
        [dic setObject:self.BankID forKey:@"bank_id"];
        [dic setObject:self.FootV.JiFenText.text forKey:@"points"];
        [MainObjectWay PostWithMethod:@"points_calculation" andAction:@"Points" andAddParam:YES parameters:dic success:^(id responseObject) {
            NSLog(@"返回兑换数据 = %@",responseObject);
            if ([MainObjectWay IsSucceedWithCode:responseObject[@"code"]]) {
                self.FootV.JiFenLabel.text = [NSString stringWithFormat:@"%@",responseObject[@"data"][@"money"]];
            }
            
        } failure:^(NSError *error) {
            NSLog(@"错误信息 = %@",error);
        }];
    }];
}

- (void)ClickOnBtnToDuiHuan{
    
    DuiHuanNewsController * DuiHuanN = [[DuiHuanNewsController alloc] init];
    DuiHuanN.BankID = self.BankID;
    DuiHuanN.DataArr = self.JiFenArr;
    [self.navigationController pushViewController:DuiHuanN animated:YES];
    
}

- (NSMutableArray *)JiFenArr {
    if (!_JiFenArr ) {
        _JiFenArr = [NSMutableArray array];
    }
    return _JiFenArr;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
